﻿
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using Swaf;
using Swaf.Gui;

using Swaf.BizObj;
using Swaf.Container;
using Swaf.Security;

namespace FilterSort.Gui
{
	public class ArticleSearchBizProc : BaseBusinessProcess
	{
		public ArticleSearchBizProc(ActiveBizProc procState)
			:base(procState)
		{
			setupNewSearch(false);
		}

        protected override void setupRuleNames()
		{
		}

		public override Privileges CurrentEditPrivilege
		{
			get 
			{
				Privileges priv = Privileges.Add;
				return priv;
			}
		}

		protected override string BizObjTypeName {get{return "ArticleKeyword";}}

		public override IResults updateBizObj(IBizObj obj)
		{
			IResults rc = null;
			ActiveBizProc pushCrit = m_procState.getParentBizProc("pushCriteria");
			if (pushCrit != null)
			{
				string keyword = (string)CurrentBizObj.get("keyword");
				rc = App.ruleMgr.execute("getFilterSortCriteriaFromKeyword", keyword, CurrentBizObj.get("onlyCatagories"));
				pushCrit.BizProc.FilterSortCriteria = rc.CallResults as IBizObj;
				pushCrit.BizProc.clearRetrievedData();
			}
			else
			{
				string keyword = (string)CurrentBizObj.get("keyword");
				Swaf.BGuid.Guid prodId = null;
				if (Swaf.BGuid.Guid.TryParse(keyword, out prodId))
				{
					string nav = m_procState.get("idNav") as string;
					if (!Cmn.IsEmpty(nav))
					{
						rc = App.ruleMgr.execute("getProductDetails", prodId, !App.resMgr.UsingBuiltInCulture,DetailLevel.idAndTitle,false,true);
						IBizObj details = rc.CallResults as IBizObj;
						if (details != null)
						{
							nav = App.resMgr.resolve(nav, new NameAddressSupportDictionaryWrapper(details));
							m_procState.BPC.HostView.gotoView(nav);
						}

					}
				}
				else
				{
					string nav = m_procState.get("searchNav") as string;
					if (!Cmn.IsEmpty(nav))
					{
						keyword = Uri.EscapeDataString(keyword);
						m_procState.BPC.HostView.gotoView(App.resMgr.resolve(nav).Replace("{0}", keyword));
					}
				}
			}
			return rc;
		}
		public override void updateBizObj(bool refreshOnly)
		{
			IBizObj srch = State.InProgressAddBizObj;
			base.updateBizObj(refreshOnly);
			State.InProgressAddBizObj = srch;
		}
		public override void cancelEdits()
		{
			base.cancelEdits();
			ActiveBizProc pushCrit = m_procState.getParentBizProc("pushCriteria");
			if (pushCrit != null)
			{
				pushCrit.BizProc.FilterSortCriteria = null;
				pushCrit.BizProc.clearRetrievedData();
			}
			setupNewSearch(true);
		}
		public override void updateActionState()
		{
			base.updateActionState();
			if (Cmn.IsEmpty(CurrentBizObj.get("keyword")))
				((GenericAction)Actions["CancelEdits"]).Availability = ActionAvailability.none;
		}

		protected void setupNewSearch(bool force)
		{
			if (force || State.InProgressAddBizObj == null)
			{
				State.InProgressAddBizObj = createNewBizObj();
				string initSearch = m_procState.get("keyword") as string;
				if (!Cmn.IsEmpty(initSearch))
				{
					CurrentBizObj.put("keyword", initSearch);
					updateBizObj(CurrentBizObj);
				}

			}
		}
	}
}
